package com.example.testnetdemo.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.CellIdentityCdma;
import android.telephony.CellIdentityGsm;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityWcdma;
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WangJinyong on 2018/3/31.
 * 获取SIM卡信息和手机号码
 */

public class PhoneInfoUtils {

    private static String TAG = "PhoneInfoUtils";

    private TelephonyManager telephonyManager;
    //移动运营商编号
    private String NetworkOperator;
    private Context context;

    public PhoneInfoUtils(Context context) {
        this.context = context;
        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }

    //获取sim卡iccid
    public String getIccid() {
        String iccid = "N/A";
        iccid = telephonyManager.getSimSerialNumber();
        return iccid;
    }

    //获取电话号码
    public String getNativePhoneNumber() {
        String nativePhoneNumber = "N/A";
        nativePhoneNumber = telephonyManager.getLine1Number();
        return nativePhoneNumber;
    }

    //获取手机服务商信息
    public String getProvidersName() {
        String providersName = "N/A";
        NetworkOperator = telephonyManager.getNetworkOperator();
        //IMSI号前面3位460是国家，紧接着后面2位00 02是中国移动，01是中国联通，03是中国电信。
//        Flog.d(TAG,"NetworkOperator=" + NetworkOperator);
        if (NetworkOperator.equals("46000") || NetworkOperator.equals("46002")) {
            providersName = "中国移动";//中国移动
        } else if (NetworkOperator.equals("46001")) {
            providersName = "中国联通";//中国联通
        } else if (NetworkOperator.equals("46003")) {
            providersName = "中国电信";//中国电信
        }
        return providersName;

    }

    public String getPhoneInfo() {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        StringBuilder sb = new StringBuilder();

        assert tm != null;
        sb.append("手机SIM基站信息：\n");
        sb.append("\nNetworkOperatorName（网络运营商名称）= ").append(tm.getNetworkOperatorName());//移动运营商名称
        sb.append("\nSimCountryIso（SIM运营商国家代码）= ").append(tm.getSimCountryIso());
        sb.append("\nSimOperator（SIM运营商代码）= ").append(tm.getSimOperator());
        sb.append("\nSimOperatorName（SIM运营商名称）= ").append(tm.getSimOperatorName());
        sb.append("\nSimSerialNumber（SIM卡的序列号）= ").append(tm.getSimSerialNumber());
        sb.append("\nSubscriberId(IMSI)（SIM卡序列号）= ").append(tm.getSubscriberId());

        //电信用的是cdma制式网络，移动和联通用的gsm网络
        if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
            CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) telephonyManager.getCellLocation();
            int bid = cdmaCellLocation.getBaseStationId(); //获取cdma基站识别标号 BID
            int nid = cdmaCellLocation.getNetworkId(); //获取cdma网络编号NID
            int sid = cdmaCellLocation.getSystemId(); //用谷歌API的话cdma网络的mnc要用这个getSystemId()取得→SID

            sb.append("\nBID = ").append(bid);
            sb.append("\nNID = ").append(nid);
            sb.append("\nSID = ").append(sid);
        } else {
            GsmCellLocation gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
            if(gsmCellLocation!=null){
                int cid = gsmCellLocation.getCid(); //获取gsm基站识别标号
                int lac = gsmCellLocation.getLac(); //获取gsm网络编号
                int psc = gsmCellLocation.getPsc();

                sb.append("\nCID = ").append(cid);
                sb.append("\nLAC = ").append(lac);
                sb.append("\nPSC = ").append(psc);
            }
        }

        List<CellInfo> infos = telephonyManager.getAllCellInfo();
        if (infos != null) {
            sb.append("\n\n附近基站信息个数 = ").append(infos.size()).append("\n");
            for (CellInfo i : infos) { // 根据邻区总数进行循环
                if (i instanceof CellInfoGsm) {//这里的塔分为好几种类型
                    sb.append("\ngsm基站:");
                    CellIdentityGsm cellIdentityGsm = ((CellInfoGsm) i).getCellIdentity();//从这个类里面可以取出好多有用的东西
                    sb.append("\nLAC = ").append(cellIdentityGsm.getLac());
                    sb.append("\nMCC = ").append(cellIdentityGsm.getMcc());
                    sb.append("\nMNC = ").append(cellIdentityGsm.getMnc());
                    sb.append("\nCID = ").append(cellIdentityGsm.getCid()).append("\n");

                } else if (i instanceof CellInfoCdma) {
                    sb.append("\ncdma基站:");
                    CellIdentityCdma cellIdentityCdma = ((CellInfoCdma) i).getCellIdentity();

                    sb.append("\nBID = ").append(cellIdentityCdma.getBasestationId());
                    sb.append("\nNID = ").append(cellIdentityCdma.getNetworkId());
                    sb.append("\nSID = ").append(cellIdentityCdma.getSystemId()).append("\n");

                } else if (i instanceof CellInfoLte) {
                    sb.append("\nlte基站:");
                    CellIdentityLte cellIdentityLte = ((CellInfoLte) i).getCellIdentity();
                    sb.append("\nTAC = ").append(cellIdentityLte.getTac());
                    sb.append("\nMCC = ").append(cellIdentityLte.getMcc());
                    sb.append("\nMNC = ").append(cellIdentityLte.getMnc());
                    sb.append("\nCI = ").append(cellIdentityLte.getCi()).append("\n");
                } else if (i instanceof CellInfoWcdma) {
                    sb.append("\nwcdma基站:");
                    CellIdentityWcdma cellIdentityWcdma = ((CellInfoWcdma) i).getCellIdentity();
                    if (cellIdentityWcdma == null) continue;
                    sb.append("\nLAC = ").append(cellIdentityWcdma.getLac());
                    sb.append("\nCID = ").append(cellIdentityWcdma.getCid());
                    sb.append("\nMCC = ").append(cellIdentityWcdma.getMcc());
                    sb.append("\nMNC = ").append(cellIdentityWcdma.getMnc()).append("\n");
                }
            }
        }
        return sb.toString();
    }
}
